package render;

import java.util.Vector;

import org.lwjgl.LWJGLException;

import world.Chunk;

public class Render {

	
	
	private boolean firstHasChunk=false;
	private Vector<Chunk> chunk = new Vector<Chunk>();
	
	private Vector<RenderChunk> chunkRenderers = new Vector<RenderChunk>();
	
	public Render()
	{
		
		//define the first chunk render instance to ensure that it is initialized.
		//each will be given a single chunk for it to handle using the existing code
	}
	
	public void addChunks(Vector<Chunk> chunkList)
	{
		//go through each chunk and see if it is already in a instance of renderChunk or not
		//if its not make a new one and add it to the list of chunks to render
		for (int i=0;i<chunkList.size();i++)
		{
			
			//check to see if the chunk exists in a existing renderer
			boolean exists=false;
			int chunkAddressInRenderer=0;//initialize to 0.
			
			for(int x=0;x<chunkRenderers.size();x++)
			{
				if(chunkRenderers.elementAt(x).getRenderList().firstElement().equals(chunkList.elementAt(i)))
				{
					//if the chunk exists in one of the chunkRender objects
					exists=true;
					chunkAddressInRenderer=x;//store the address of this chunk in the list
					
				}
				
					//if the chunk does not exist then exists will be false and we will make a new one
				
			}
			Vector<Chunk> temp = new Vector<Chunk>();
			temp.add(chunkList.elementAt(i));
			if(exists)
			{
				
			//Check if the rendering of the chunk needs to be updated
			if(chunkRenderers.elementAt(chunkAddressInRenderer).getRenderList().firstElement().isWaitingToBeRendered())
			{
				chunkRenderers.elementAt(chunkAddressInRenderer).setRenderList(temp);
				//replace chunk with updated version
				chunkRenderers.elementAt(chunkAddressInRenderer).getRenderList().firstElement().setRendered(true);
				//the chunk is rendered
				
				chunkRenderers.elementAt(chunkAddressInRenderer).getRenderList().firstElement().setWaitingToBeRendered(false);
				//the chunk is not waiting to be rendered
				
			}
			
			
			}
			else
			{
				
				
				//move the chunk into the chunkList of that render class
				
				chunkRenderers.add(new RenderChunk());
				chunkRenderers.lastElement().setRenderList(temp);
				
				
				
			}//
			
			
		}//
		
		//System.err.println("Number of Chunks to render:"+ chunkRenderers.size());
	}
	
	public void setUpWindow()
	{
		try {
			chunkRenderers.firstElement().setUpWindow();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setUpOpenGL()
	{
		//initialize open gl parameters
		for(int i=0; i< chunkRenderers.size();i++)
		{
		chunkRenderers.elementAt(i).initVBOOpenGL();
		}
	
	}
	
	public void setUpWorldSize(int worldSize)
	{
		
		for (int i=0;i<chunkRenderers.size();i++)
		{
			chunkRenderers.elementAt(i).setWorldSize(worldSize);
		}
	}
	
	public void createChunkData()
	{
		
		for (int i=0;i<chunkRenderers.size();i++)
		{
			chunkRenderers.elementAt(i).createVBOData();
		}
		
	}
	
	public void render()
	{
		chunkRenderers.elementAt(0).clear();
		for (int i=0;i<chunkRenderers.size();i++)
		{
		chunkRenderers.elementAt(i).transformations();
		}
		
		for (int i=0;i<chunkRenderers.size();i++)
		{
			chunkRenderers.elementAt(i).renderVBO();
		}
		
		for (int i=0;i<chunkRenderers.size();i++)
		{
		chunkRenderers.elementAt(0).popMatrix();
		}
	}
	
	
	
}
